package com.xms.web.api;

import com.xms.configuration.model.PlatformSetting;
import com.xms.configuration.service.ISettingFactory;
import com.xms.core.annotation.AllowAnonymous;
import com.xms.core.org.UserContext;
import com.xms.utils.JsonUtil;
import com.xms.web.framework.controller.ApiBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * WebContextController
 * </p>
 *
 * @author migo
 * @since 2021/10/23
 */
@RestController
@RequestMapping({"/{org}/api/webcontext", "/api/webcontext"})
public class WebContextController extends ApiBaseController {
    private ISettingFactory _settingFactory;
    @Autowired
    public void WebContextController(ISettingFactory settingFactory) {
        _settingFactory = settingFactory;
    }

    @AllowAnonymous
    @GetMapping("")
    public String get(){
        StringBuilder result = new StringBuilder();
        result.append("var CURRENT_USER = ");
        UserContext user = _currentUser.getUser();
        if(user != null){
            result.append(JsonUtil.toJSONString(user));
        }
        else{
            result.append("null");
        }
        result.append(";");
        result.append("var APP_SETTING = ");
        try {
            result.append(JsonUtil.toJSONString(_settingFactory.getObject(PlatformSetting.class)));
        }catch(Exception e){
            result.append(JsonUtil.toJSONString(new PlatformSetting()));
        }
        return result.toString();
    }
}
